package com.tevintao.marketingsystem.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.SystemClock;

import com.tevintao.marketingsystem.R;

public class ActTestTouchEvent extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private Button mStartBtn, mBtn1, mBtn2;

    private ListView mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_test_touch_event);
        mStartBtn = (Button) findViewById(R.id.test_start);
        mBtn1 = (Button) findViewById(R.id.test_btn1);
        mBtn2 = (Button) findViewById(R.id.test_btn2);
        mStartBtn.setOnClickListener(this);
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);

        mList = (ListView) findViewById(R.id.test_list);
        TextView header = new TextView(this);
        header.setTag("header");
        TextView footer = new TextView(this);
        footer.setTag("footer");
        mList.addHeaderView(header);
        mList.addFooterView(footer);
        mList.setAdapter(adapter);
        mList.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.test_start:
                mBtn1.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, mBtn1.getLeft() + 5, mBtn1.getTop() + 5, 0));
                mBtn1.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, mBtn1.getLeft() + 5, mBtn1.getTop() + 5, 0));
                break;
            case R.id.test_btn1:
                Toast.makeText(this, "按钮1", Toast.LENGTH_LONG).show();
                break;
            case R.id.test_btn2:

//                int left = mList.getLeft();
//                int right = mList.getRight();
                int first = mList.getFirstVisiblePosition();
                int last = mList.getLastVisiblePosition();
                Toast.makeText(this, "按钮2" + " first = " + first + " last = " + last, Toast.LENGTH_LONG).show();
                int position = 0;
                int headerCount = mList.getHeaderViewsCount();
                int selected = position + headerCount;
                mList.performItemClick(mList.getChildAt(selected), selected, mList.getItemIdAtPosition(selected));
                break;
        }
    }


    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return 100;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(ActTestTouchEvent.this);
            tv.setText("position= " + position);
            tv.setTag(position);
            return tv;
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "listView click " + position + " tag = " + view.getTag(), Toast.LENGTH_LONG).show();
    }
}
